//
//  FMDBManager.m
//  takeout
//
//  Created by Mac on 12-3-23.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "FMDBManager.h"
#import "FMDatabase.h"
#import "SupplierCategory.h"
@implementation FMDBManager
@synthesize db;
//初始化数据库，创建数据库，表
- (void)initDataBase{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
    NSString *documentDirectory = [paths objectAtIndex:0];  
    NSString *dbPath = [documentDirectory stringByAppendingPathComponent:@"TakeOut.db"];
    db= [FMDatabase databaseWithPath:dbPath] ;  
    if (![db open]) { 
        NSLog(@"Could not open db."); 
    }
    if([db open]){
        NSLog(@"open db.");
    }
    //商铺类型
    [db executeUpdate:@"CREATE TABLE supplier_category IF NOT EXISTS supplier_category (id integer, name text)"];
    //商铺
    [db executeUpdate:@"CREATE TABLE supplier IF NOT EXISTS supplier (id integer, name text, address text, map_address text,longtitude float,latitude float,phone text,jointime text,starttime integer,endtime integer,leastprice float,leastdistance float,deliverydesc text,avgprice float,consumerecord integer,picture text,desc text,sup_cate_id integer,city integer,phone2 text,telephone text,telephone2 text,road text)"];
    //菜系
    [db executeUpdate:@"CREATE TABLE dish_category IF NOT EXISTS dish_category (id integer, name text, supplierid integer)"];
    //菜品
    [db executeUpdate:@"CREATE TABLE dish IF NOT EXISTS dish (id integer, name text, picture text, price float, desc text, dish_cate_id integer)"];
    //收藏
    [db executeUpdate:@"CREATE TABLE collections IF NOT EXISTS collections (id integer not null, supplierid integer,name text,picture text,primary key(id))"];
    //浏览纪录
    [db executeUpdate:@"CREATE TABLE transaction_historys IF NOT EXISTS transaction_historys(id integer not null, supplierid integer,name text,picture text,primary key(id))"];
    [db executeUpdate:@"CREATE TABLE bunddle_item IF NOT EXISTS bunddle_item (id integer, name text, picture text, originalprice float, discountprice float, quantity integer, desc text, bunddleid integer, supplierid integer)"];
    [db executeUpdate:@"CREATE TABLE bunddle IF NOT EXISTS bunddle (id integer, name text, price float, picture text, desc text, supplierid integer)"];
    NSLog(@"创建表");
}
-(FMDatabase *)getDataBase{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);  
    NSString *documentDirectory = [paths objectAtIndex:0];  
    NSString *dbPath = [documentDirectory stringByAppendingPathComponent:@"TakeOut.db"];
    db= [FMDatabase databaseWithPath:dbPath] ;  
    [db open];
    return db;
}
//关闭数据库
-(void)closeDataBase{
    [db close];
}


@end
